class Test {
    constructor() {}
    hw1() {
        var str = `fjskfjsl fjkdsjfkls fjklsfjlsd127.0.0.1fjksldfjafjio jfk192.168.1.101sljfs fsdjfkldsfjsklfsklffjoijfoiaf
    asfjs fed jfjf jf sdjfsi jfi jafis function dd() { } afjsdiajf iosa function abc() { } jfisjad fjsaiofjaoissf
    addedfds`;
        const numRegExp = "0|[1-9]\\d{0,2}",
            varRegExp = "[a-zA-Z_$][a-zA-Z_$0-9]*",
            paramRegExp = `\\s*|\\s*${varRegExp}\\s*(\\,\\s*${varRegExp}\\s*)*`;
        let regExpIPV4 = new RegExp(`((${numRegExp})\.){3}(${numRegExp})`, "g"),
            //regExpIPV4 = /((0|[1-9]\d*)\.){3}(0|[1-9]\d*)/g,
            regExpFn = new RegExp(`function\\s+${varRegExp}\\s*\\((${paramRegExp})\\)\\s*\\{\\s*\\}`,"g");
            // ${paramRegExp}
        // to do: how to match valid elements in "{}"

        let result = str.match(regExpIPV4);
        console.log(result);

        let result2 = str.match(regExpFn);
        console.log(result2);
    }
    hw2_Fn(arr1, arr2) {
        if (!Array.isArray(arr1) || !Array.isArray(arr2)) throw "The input parameter is invalid!";
        let res = [];
        let i = 0,
            j = 0,
            index = 0;
        while (i < arr1.length && j < arr2.length) {
            res[index++] = arr1[i++];
            res[index++] = arr2[j++];
        }
        return res.concat(arr1.slice(i)).concat(arr2.slice(j));
    }
    shaizi(n = 1) {
        if (n === undefined || typeof n !== "number") throw "The input number is invalid";
        const max = 6 * n;
        console.log(Math.ceil(Math.random() * (max - 1)));
    }
    afn() {
        function sleep(time) {
            return new Promise((resolve, reject) => {
                setTimeout(function() {
                    resolve(console.log(`I have slept for ${time} s!`));
                },time);
            });
        }

        async function testasync() {
            try {
                await sleep(2000);
                await sleep(1000);
            }catch(error){
                console.log(error);
            }
        }
        testasync();
    }
}

// for test!
let test = new Test();
test.hw1();
console.log(test.hw2_Fn([1, 2, 3], [4, 5, 6]));
test.shaizi();
test.afn();


